package com.netted.operationassistant.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.android.volley.VolleyError;
import com.blankj.utilcode.util.StringUtils;
import com.mylhyl.superdialog.SuperDialog;
import com.netted.operationassistant.R;
import com.netted.operationassistant.constant.Constant;
import com.netted.operationassistant.entity.Port;
import com.netted.operationassistant.http.HttpRequest;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class ReturnTipsActivity extends BaseActivity {

    TextView ssTv;
    Handler handler ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_return_tips);

        ssTv = (TextView)findViewById(R.id.ss_ReturnTipsActivity);
        handler= new Handler();

    }


    @Override
    protected void onResume() {
        super.onResume();



        final String from = getIntent().getStringExtra("from");
        final String portStr = getIntent().getStringExtra("portStr");

//        timeThread.start();



        new Thread(timeRunnable).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                check(from,portStr);
            }
        }).start();

    }

    Runnable timeRunnable = new Runnable() {
        @Override
        public void run() {
            int i = 0;

            while(i <= 20){
                final int count = i;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ssTv.setText(("("+(20-count))+"S)");
                    }
                });
                try{
                    Thread.sleep(1000);
                    i ++;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    };





    public void check(String from,String portStr){

        int i = 0;
        boolean isSay = false;
        while(i <20){
            Port port = getPortStatus(portStr);
            if (port != null){
                if("N".equals(port.getIsOpen()) && "Y".equals(port.getIsHas())){
                    i --;
                    String catbinetId = sharedPreferencesHelper.getSharedPreference(Constant.XML_CATBINET_ID,"")+"";
                    if("BXHJActivity".equals(from)){
                        returnSuccessByRepair(catbinetId,portStr);
                    }else if ("ReturnDeviceActivity".equals(from)){
                        returnSuccess(catbinetId,portStr);
                    }

                    return;
                }else if(!isSay && "Y".equals(port.getIsOpen()) && "Y".equals(port.getIsHas())){
                    playSound(ReturnTipsActivity.this,R.raw.s3);
                    isSay = true;
                }else if("N".equals(port.getIsOpen()) && "N".equals(port.getIsHas())){
                    playSound(ReturnTipsActivity.this,R.raw.s14);
                    showDialog("提示", "仓门已关闭,但没检测到平板入仓,归还平板失败", new SuperDialog.OnClickPositiveListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(ReturnTipsActivity.this,MenuActivity.class);
                            startActivity(intent);
                            ReturnTipsActivity.this.finish();
                        }
                    });
                    return;
                }
            }

            i ++;
            try{
                Thread.sleep(700);

            }catch (Exception e){
                e.printStackTrace();
            }

        }


        showDialog("提示", "平板归还超时,请重新检测归还", new SuperDialog.OnClickPositiveListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ReturnTipsActivity.this,MenuActivity.class);
                startActivity(intent);
                ReturnTipsActivity.this.finish();
            }
        });
    }


    public void returnSuccess(String catbinetId,String port ){
        Map<String,String> params = new HashMap<>();
        params.put("catbinetId",catbinetId);
        params.put("port",port);
        request.post(Constant.HOST+"/ws/opt/returnSuccess",params, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                try{
                    if (!StringUtils.isEmpty(response)){
                        JSONObject json = JSONObject.parseObject(response);
                        String data = json.getString("data");
                        showDialog("提示", json.getString("message"), new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(ReturnTipsActivity.this,MenuActivity.class);
                                startActivity(intent);
                                ReturnTipsActivity.this.finish();
                                return;
                            }
                        });

                    }
                }catch (Exception e){
                    e.printStackTrace();
                    showDialog("提示","网络异常,请联系管理员",null);
                }
            }

            @Override
            public void onError(VolleyError error) {
                showDialog("t",error.toString(),null);
            }

        });
    }

    public void returnSuccessByRepair(String catbinetId,String port ){
        Map<String,String> params = new HashMap<>();
        params.put("catbinetId",catbinetId);
        params.put("port",port);
        request.post(Constant.HOST+"/ws/opt/returnSuccessByRepair",params, "post", new HttpRequest.Callback() {
            @Override
            public void onSuccess(String response) {
                try{
                    if (!StringUtils.isEmpty(response)){
                        JSONObject json = JSONObject.parseObject(response);
                        String data = json.getString("data");
                        showDialog("提示", json.getString("message"), new SuperDialog.OnClickPositiveListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(ReturnTipsActivity.this,MenuActivity.class);
                                startActivity(intent);
                                ReturnTipsActivity.this.finish();
                                return;
                            }
                        });
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    showDialog("提示","网络异常,请联系管理员",null);
                }
            }

            @Override
            public void onError(VolleyError error) {
                showDialog("t",error.toString(),null);
            }

        });
    }

}
